﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Admin;
using System.Windows.Markup;

namespace SL.Participant
{
    public partial class Page : UserControl
    {
        public delegate void NextPageClickedDelegate(List<string> Names, List<string> Values);
        public event NextPageClickedDelegate NextPageClicked;
        public delegate void PreviousPageClickedDelegate();
        public event PreviousPageClickedDelegate PreviousPageClicked;
        public bool StudyLoaded { get; set; }
        public Grid MainGrid
        {
            get
            {
                return LayoutRoot;
            }
            set
            {
                LayoutRoot = value;
            }

        }
        public Object MainContent
        {
            get { return ccSurvey.Content; }
            set { ccSurvey.Content = value; }
        }
        public Button PreviousButton
        {
            get
            {
                return btnBack;
            }
            set
            {
                btnBack = value;
            }
        }
        public Button NextButton
        {
            get
            {
                return btnNext;
            }
            set
            {
                btnNext = value;
            }
        }
        public Page()
        {
            InitializeComponent();
        }
        double _originalWidth = -1;
        double _originalHeight = -1;
        public void AddContent(string PageContent)
        {
            string Content = Common.StudyManager.XamlInitializationString.Replace("{0}", PageContent) ;
            UIElement ui = XamlReader.Load(Content) as UIElement;
            ccSurvey.Content = ui;
            AddValidationHandlers((FrameworkElement)ccSurvey.Content);
            //Do the intial validation
            StudyLoaded = true;
            btnNext.IsEnabled = Validate();
            
        }
        public void AddContent(object Content)
        {
            ccSurvey.Content = Content;
            AddValidationHandlers((FrameworkElement)ccSurvey.Content);
            //Do the intial validation
            StudyLoaded = true;
            btnNext.IsEnabled = Validate();

        }
        public void AddContent(UIElement Control)
        {
            ccSurvey.Content = Control;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            List<string> Names = new List<string>();
            List<string> Values = new List<string>();
            SaveValuesRecursive((FrameworkElement)ccSurvey.Content, Names, Values);            
            NextPageClicked(Names,Values);
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            PreviousPageClicked();
        }
        private void SaveValuesRecursive(FrameworkElement e, List<string> Names, List<string> Values)
        {
            if (e is Slider)
            {
                Slider mySlider = (Slider)e;
                Names.Add(mySlider.Name);
                Values.Add(mySlider.Value.ToString());
            }
            else if (e is ListBox)
            {
                ListBox myListBox = (ListBox)e;
                Names.Add(myListBox.Name);
                Values.Add(myListBox.SelectedIndex.ToString());
            }
            else if (e is DatePicker)
            {
                DatePicker myDatePicker = (DatePicker)e;
                Names.Add(myDatePicker.Name);
                Values.Add(myDatePicker.SelectedDate.ToString());
            }
            else if (e is Calendar)
            {
                Calendar myDatePicker = (Calendar)e;
                Names.Add(myDatePicker.Name);
                Values.Add(myDatePicker.SelectedDate.ToString());
            }
            else if (e is CheckBox)
            {
                CheckBox myCheckBox = (CheckBox)e;
                Names.Add(myCheckBox.Name);
                Values.Add(myCheckBox.IsChecked.ToString());
            }            
            else if (e is RadioButton)
            {
                RadioButton myRadioButton = (RadioButton)e;
                if (myRadioButton.IsChecked.HasValue && myRadioButton.IsChecked.Value)
                {
                    Names.Add(myRadioButton.GroupName);
                    Values.Add(myRadioButton.Tag.ToString());
                }
            }
            else if (e is TextBox)
            {
                TextBox myTextBox = (TextBox)e;
                Names.Add(myTextBox.Name);
                Values.Add(myTextBox.Text);
            }
            //Layout Controls
            else if (e is Panel)
            {
                Panel myPanel = (Panel)e;
                foreach (object ui in myPanel.Children)
                {
                    if (ui is FrameworkElement)
                    SaveValuesRecursive((FrameworkElement)ui, Names, Values);
                }
            }
            else if (e is Border)
            {
                Border myPanel = (Border)e;
                if (myPanel.Child != null && myPanel.Child is FrameworkElement)
                {
                    SaveValuesRecursive((FrameworkElement)myPanel.Child, Names, Values);
                }
            }
            else if (e is ItemsControl)
            {
                ItemsControl myTabControl = (ItemsControl)e;
                foreach (object myItemControl in myTabControl.Items)
                {
                    if (myItemControl is FrameworkElement)
                    SaveValuesRecursive((FrameworkElement)myItemControl, Names, Values);
                }
            }
            else if (e is ContentControl)
            {
                ContentControl cc = (ContentControl)e;
                if (cc.Content != null && cc.Content is FrameworkElement)
                {
                    SaveValuesRecursive((FrameworkElement)cc.Content, Names, Values);
                }
            }
            else if (e is Page)
            {
                Page myPage = (Page)e;
                if (myPage.Content != null && myPage.Content is FrameworkElement)
                {
                    SaveValuesRecursive((FrameworkElement)myPage.Content, Names, Values);
                }
            }
            return;
        }
        private bool ValidateControls(FrameworkElement e
            , List<string> RadioButtonTagNames
            , List<RadioButton> RadioButtonList
            , List<FrameworkElement> ControlList)
        {
            bool Validated = true;
            if (e is Slider)
            {
                Slider mySlider = (Slider)e;
                if (mySlider.Value == ((mySlider.Maximum + mySlider.Minimum) / 2))
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is RadioButton)
            {
                RadioButton myRadioButton = (RadioButton)e;
                if (RadioButtonTagNames.Contains(myRadioButton.GroupName) == false)
                {
                    RadioButtonTagNames.Add(myRadioButton.GroupName);
                }
                if ( myRadioButton.IsChecked.Value == true)
                {
                    RadioButtonList.Add(myRadioButton);
                }
                ControlList.Add(e);
            }
            else if (e is TextBox)
            {
                TextBox myTextBox = (TextBox)e;
                if (string.IsNullOrEmpty(myTextBox.Text))
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is CheckBox)
            {
                CheckBox myCheckBox = (CheckBox)e;
                if (myCheckBox.IsChecked.HasValue == false)
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is ListBox)
            {
                ListBox myListBox = (ListBox)e;
                if (myListBox.SelectedIndex == -1)
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is DatePicker)
            {
                DatePicker myDatePicker = (DatePicker)e;
                if (myDatePicker.SelectedDate.HasValue == false)
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is Calendar)
            {
                Calendar myCalendar = (Calendar)e;
                if (myCalendar.SelectedDate.HasValue == false)
                {
                    Validated = false;
                }
                ControlList.Add(e);
            }
            else if (e is Panel)
            {
                Panel myPanel = (Panel)e;
                foreach (UIElement ui in myPanel.Children)
                {
                    if (ValidateControls((FrameworkElement)ui
                        , RadioButtonTagNames
                        , RadioButtonList
                        , ControlList) == false)
                    {
                        Validated = false;
                    }
                }
            }
            else if (e is Border)
            {
                Border myPanel = (Border)e;
                if (myPanel.Child != null && myPanel.Child is FrameworkElement)
                {
                    if (ValidateControls((FrameworkElement)myPanel.Child
                        , RadioButtonTagNames
                        , RadioButtonList
                        , ControlList) == false)
                    {
                        Validated = false;
                    }

                }
            }
            else if (e is ItemsControl)
            {
                ItemsControl cc = (ItemsControl)e;
                foreach (object ic in cc.Items)
                {
                    if (ic is FrameworkElement)
                    {
                        if (ValidateControls((FrameworkElement)ic
                          , RadioButtonTagNames
                          , RadioButtonList
                          , ControlList) == false)
                        {
                            Validated = false;
                        }
                    }
                }
            }
            else if (e is ContentControl)
            {
                ContentControl cc = (ContentControl)e;
                if (cc.Content != null && cc.Content is FrameworkElement)
                {
                    if (ValidateControls((FrameworkElement)cc.Content
                        , RadioButtonTagNames
                        , RadioButtonList
                        , ControlList) == false)
                    {
                        Validated = false;
                    }
                }
            }
            else if (e is Page)
            {
                Page myPage = (Page)e;
                if (myPage.Content != null)
                {
                    if (ValidateControls((FrameworkElement)myPage.Content
                        , RadioButtonTagNames
                        , RadioButtonList
                        , ControlList) == false)
                    {
                        Validated = false;
                    }
                }
            }
            else if (e is MediaElement)
            {
                //Add media element logic here
            }
            return Validated;
        }
        private void AddValidationHandlers(FrameworkElement e)
        {       
            if (e is Panel)
            {
                Panel myPanel = (Panel)e;
                foreach (UIElement ui in myPanel.Children)
                {
                    if (ui is FrameworkElement)
                        AddValidationHandlers((FrameworkElement)ui);
                }
            }            
            else if (e is Border)
            {
                Border myPanel = (Border)e;
                if (myPanel.Child != null && myPanel.Child is FrameworkElement)
                {
                    AddValidationHandlers((FrameworkElement)myPanel.Child);
                }
            }
            else if (e is ItemsControl)
            {
                ItemsControl myPanel = (ItemsControl)e;
                foreach (object ic in myPanel.Items)
                {
                    if (ic is FrameworkElement)
                        AddValidationHandlers((FrameworkElement)ic);
                }                                
            }
            else if (e is ContentControl)
            {
                ContentControl myPage = (ContentControl)e;
                if (myPage.Content != null && myPage.Content is FrameworkElement)
                {
                    AddValidationHandlers((FrameworkElement)myPage.Content);
                }
            } 
            else if (e is Page)
            {
                Page myPage = (Page)e;
                if (myPage.Content != null)
                {                    
                    AddValidationHandlers((FrameworkElement)myPage.Content);
                }
            }
            else if (e is Control)
            {
                Control myControl = (Control)e;
                myControl.MouseLeave += new MouseEventHandler(Control_MouseLeave);
            }
            else if (e is UIElement)
            {
                UIElement myControl = (UIElement)e;
                myControl.MouseLeave += new MouseEventHandler(Control_MouseLeave);
            } 
        }
       
        private bool Validate()
        {
            List<string> RadioButtonGroupNames = new List<string>();
            List<RadioButton> RadioButtonList = new List<RadioButton>();
            List<FrameworkElement> ControlList = new List<FrameworkElement>();
            bool Result = ValidateControls((FrameworkElement)ccSurvey.Content
                , RadioButtonGroupNames
                , RadioButtonList
                , ControlList);
            // Check to make sure there are actually controls
            // and not just text
            if (ControlList.Count == 0)
                return true;
            if (Result)
            {
                if (RadioButtonGroupNames.Count == RadioButtonList.Count)
                {
                    return true;
                }
            }
            return false;
        }
        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
           
        }
        void Control_MouseLeave(object sender, MouseEventArgs e)
        {
            if (StudyLoaded)
            {
                btnNext.IsEnabled = Validate();
            }
        }
        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            if (StudyLoaded)
            {
                btnNext.IsEnabled = Validate();
            }
        }

        private void sliderZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {            
            if (sliderZoom != null)
            {
                GridZoom.ScaleX = sliderZoom.Value;                
                GridZoom.ScaleY = sliderZoom.Value;
                if (_originalHeight == -1)
                {
                    _originalHeight = ccSurvey.ActualHeight;                   
                }
                if (_originalWidth == -1)
                {
                    _originalWidth = ccSurvey.ActualWidth;
                }
                ccSurvey.Height = _originalHeight * sliderZoom.Value;
                ccSurvey.Width = _originalWidth * sliderZoom.Value;
            }
           
        }
    }
}
